package downloadpdfCleaner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Properties;

import tools.FileTools;

import downloadpdfCleaner.FILETYPE;

import tools.CompressTools;

/**
 * Extract archived files and move them to dest root folder (No folder
 * structure).
 * 
 * @author Kevin
 * 
 */
public class DownloadedPDFCleaner {

	static String rarBinaryFP, srcRootFP, destRootFP;
	// final static String profileFP =
	// "c:\\docdoc\\profile\\PDFCleaner.properties";
	final static String profileFP = "C:\\docdoc\\profile\\PDFCleaner.properties";

	/**
	 * Initiate variables from Properties
	 */
	private static void Init() {
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(profileFP));

			rarBinaryFP = properties.getProperty("rarBinaryFP");
			srcRootFP = properties.getProperty("srcRootFP");
			destRootFP = properties.getProperty("destRootFP");

			properties.list(System.out);

			File destFolder = new File(destRootFP);
			if (!destFolder.exists()) {
				destFolder.mkdir();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Init();
		processFiles_Phase1(srcRootFP, destRootFP);
		FileTools.deleteAllEmptyFolder(srcRootFP);
		FileTools.deleteAllEmptyFolder(destRootFP);
		System.out.println("------------COMPLETED----------");
	}

	private static boolean processFiles_Phase1(String srcFP, String destFP) {

		File srcFile = new File(srcFP);
		File destFile = new File(destFP);

		if (srcFile.exists()) {
			String[] zipFP = getListOfZIPFiles(srcFile.getPath());
			String[] rarFP = getListOfRARFiles(srcFile.getPath());
			String srcBaseFP = srcFile.getPath();

			if (zipFP.length >= 1) {
				String tempfolderFP = CompressTools.createTempFolder(destFP);
				String namedTempFP = tempfolderFP + "\\" + srcFile.getName();

				for (String each : zipFP) {
					CompressTools.unzip(srcBaseFP + "\\" + each, namedTempFP);
				}

				processFiles_Phase1(namedTempFP, destRootFP);
				CompressTools.deletedTempFiles(tempfolderFP);
			}

			if (rarFP.length >= 1) {
				// String tempfolderFP = CompressTools.createTempFolder(destFP);
				// String namedTempFP = tempfolderFP + "\\" + srcFile.getName();
				String namedTempFP = destFP + "\\" + srcFile.getName();
				File tempFolder = new File(namedTempFP);
				tempFolder.mkdir();
				System.out.println(namedTempFP);

				for (String each : rarFP) {
					CompressTools.unrar(rarBinaryFP, "x", srcBaseFP + "\\" + each , namedTempFP );
				}

				processFiles_Phase1(namedTempFP, destRootFP);
				tempFolder = null;
			}

			File[] files = srcFile.listFiles();
			for (File each : files) {
				if (each.isDirectory()) {
					processFiles_Phase1(each.getPath(), destFP);

				} else {

					String parentFolderName = FileTools.getParentFolderName(each.getPath());
					FILETYPE ft = FILETYPE.getFileType(FileTools.getFileExt(each.getName()));

					switch (ft) {
					case JPG:
					case M4U:
					case M4A:
					case WMV:
					case AVI:
					case DOC:
					case AAC:
					
					case CBR:
					case PPT:
					case XLSX:
					case XLS:
					case PPTX:
					case DOCX:
					case MOBI:
					case EPUB:
					case MP3:
					case MP4:
						File newFolderFile = new File(destRootFP + "\\" + parentFolderName);

						if (!newFolderFile.exists())
							newFolderFile.mkdir();

						System.out.println(">> MP3: 	" + destRootFP + "\\" + parentFolderName + "\\" + each.getName());
						each.renameTo(new File(destRootFP + "\\" + parentFolderName + "\\" + each.getName()));
						break;

					case PDF:
						File newDocument;
						if (each.getParent().equalsIgnoreCase(srcRootFP))
							newDocument = new File(destFile + "\\" + each.getName());
						else if (parentFolderName.length() > each.getName().length())
							newDocument = new File(destFile + "\\" + parentFolderName + "." + ft.getExt());
						else
							newDocument = new File(destFile + "\\" + each.getName());

						if (newDocument.exists()) {
							String parent_path = newDocument.getParent();
							String newDocument_name = newDocument.getName();
							String new_newDocument_name = tools.FileTools.getFileNameWithoutExt(newDocument_name)
									+ "(Anohter)" + tools.FileTools.getFileExt(newDocument_name);
							newDocument = new File(parent_path + "\\" + new_newDocument_name);
						}

						each.renameTo(newDocument);

						newDocument = null;
						break;
					default:
						System.out.println(">> Deleted: 	" + each.getPath());
						each.delete();
						break;
					}
				}
			}
		}

		if (!srcFile.getParent().equalsIgnoreCase(srcRootFP))
			return (srcFile.delete());
		else
			return true;
	}

	private static String[] getListOfZIPFiles(String aDirectory) {
		// our filename filter (filename pattern matcher)
		class OnlyUnderscoreFilter implements FilenameFilter {
			public boolean accept(File dir, String s) {
				if (s.toLowerCase().endsWith(".zip"))
					return true;
				return false;
			}
		}
		return new java.io.File(aDirectory).list(new OnlyUnderscoreFilter());
	}

	private static String[] getListOfRARFiles(String aDirectory) {
		// our filename filter (filename pattern matcher)
		class OnlyUnderscoreFilter implements FilenameFilter {
			public boolean accept(File dir, String s) {
				if (s.toLowerCase().endsWith(".rar"))
					return true;
				return false;
			}
		}
		return new java.io.File(aDirectory).list(new OnlyUnderscoreFilter());
	}
}
